package com.yehui.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect//说明是切面
public class LogAspect {
    /**
     * 前置通知
     * @param joinPoint
     */
    @Before("execution(* com.yehui.aop.HelloService.sayHello(..))")
    public void logStart(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("logSart()=========>"+name+"");
    }

    /**
     * 后置通知
     * @param joinPoint
     */
    @After("execution(* com.yehui.aop.HelloService.sayHello(..))")
    public void logAfter(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("logAfter()=========>"+name+"");
    }

    /**
     * 返回通知
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @AfterReturning(value = "execution(* com.yehui.aop.HelloService.sayHello(..))",returning = "result")
    public Object logAfterReturning(JoinPoint joinPoint,String result) throws Throwable {
        String name = joinPoint.getSignature().getName();
        System.out.println("logAfterReturning()=========>"+name+"");
        System.out.println(result);
        return result;
    }

    /**
     * 异常通知
     * @param joinPoint
     * @param e
     * @return
     * @throws Throwable
     */
    @AfterThrowing(value = "execution(* com.yehui.aop.HelloService.sayHello(..))",throwing = "e")
    public void logAfterThrowin(JoinPoint joinPoint,Exception e ) throws Throwable {
        String name = joinPoint.getSignature().getName();
        System.out.println("AOP logAfterThrowin Advice..." + e);
        System.out.println("logAfterThrowin...");
    }

    @Around("execution(* com.yehui.aop.HelloService.sayHello(..))")
    public void around(ProceedingJoinPoint pjp){
        System.out.println("AOP Aronud before...");
        try {
            pjp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("AOP Aronud after...");
    }
}
